using Circus.CodeEditor;
using Circus.CodeEditor.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace Circus.CodeEditor
{
	public class MacroKeyList : List<MacroKeyData>, IList<MacroKeyData>, ICollection<MacroKeyData>, IEnumerable<MacroKeyData>, IEnumerable
	{
	
		private object FindTarget(object owner, string target)
		{
			object result;
			if (owner != null)
			{
				if (owner.GetType().Name == target)
				{
					result = owner;
					return result;
				}
				PropertyInfo[] properties = owner.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
				for (int i = 0; i < properties.Length; i++)
				{
					PropertyInfo propertyInfo = properties[i];
					if (propertyInfo.PropertyType.Name == target || propertyInfo.PropertyType.Name == "I" + target)
					{
						result = propertyInfo.GetValue(owner, null);
						return result;
					}
				}
			}
			result = null;
			return result;
		}
		public void LinkMacros(object owner, EventHandlers keyHandlers)
		{
			foreach (MacroKeyData current in this)
			{
				if (current.FullName != null && current.FullName != string.Empty)
				{
					current.Action = null;
					current.ActionEx = null;
					int num = current.FullName.LastIndexOf('.');
					if (num >= 0)
					{
						object obj = this.FindTarget(owner, current.FullName.Substring(0, num));
						if (obj != null)
						{
							string b = current.FullName.Substring(num + 1);
							FieldInfo[] fields = keyHandlers.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
							for (int i = 0; i < fields.Length; i++)
							{
								FieldInfo fieldInfo = fields[i];
								object value = fieldInfo.GetValue(keyHandlers);
								if (value is KeyEvent)
								{
									KeyEvent keyEvent = (KeyEvent)value;
									if (keyEvent.Target == obj && keyEvent.Method.Name == b)
									{
										current.Action = keyEvent;
										break;
									}
								}
								if (value is KeyEventEx)
								{
									KeyEventEx keyEventEx = (KeyEventEx)value;
									if (keyEventEx.Target == obj && keyEventEx.Method.Name == b)
									{
										current.ActionEx = keyEventEx;
										break;
									}
								}
							}
						}
					}
				}
				if (current.ParamType != KeyEventType.None && current.ParamStr != string.Empty)
				{
					switch (current.ParamType)
					{
					case KeyEventType.IntParam:
						current.Param = int.Parse(current.ParamStr);
						break;
					case KeyEventType.CharParam:
						current.Param = (char)int.Parse(current.ParamStr);
						break;
					case KeyEventType.SelectionParam:
						if (string.Compare(current.ParamStr, SelectionType.Block.ToString(), true) == 0)
						{
							current.Param = SelectionType.Block;
						}
						else
						{
							if (string.Compare(current.ParamStr, SelectionType.Stream.ToString(), true) == 0)
							{
								current.Param = SelectionType.Stream;
							}
							else
							{
								current.Param = SelectionType.None;
							}
						}
						break;
					}
				}
			}
		}
	}
}
